﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace QT.SEO
{
    /// <summary>
    /// 操作搜索引擎的类
    /// </summary>
    public class SearchEngine
    {
        /// <summary>
        /// 获取搜索引擎列表（含名称和相关设置）
        /// </summary>
        /// <returns>DataTable</returns>
        public static DataTable GetAll()
        {
            string sql = "select [sid],[SearchName],[URI],[ResultRegex],[SectionRegex],[HtmlRegex],[SnapRegex],[CodePage],[CodeName],[PageRegex],[CountRegex] from [QT_Basic_SearchEngine]";

            DataTable dt = new QT.DBO.OleDBHelper(myDB.connstring).DataTable(sql);

            return dt;
        }

        /// <summary>
        /// 获取搜索引擎名称列表，
        /// </summary>
        /// <returns>DataTable</returns>
        public static DataTable GetAllList()
        {
            string sql = "select [sid],[SearchName] from [QT_Basic_SearchEngine]";

            DataTable dt = new QT.DBO.OleDBHelper(myDB.connstring).DataTable(sql);

            return dt;
        }

        /// <summary>
        /// 获取指定搜索引擎的相关设置
        /// </summary>
        /// <param name="sid">搜索引擎ID</param>
        /// <returns>DataTable</returns>
        public static DataTable Get(string sid)
        {
            string sql = "select [SearchName],[URI],[ResultRegex],[SectionRegex],[HtmlRegex],[SnapRegex],[CodePage],[CodeName],[PageRegex] from [QT_Basic_SearchEngine] where [sid]='" + sid + "'";

            DataTable dt = new QT.DBO.OleDBHelper(myDB.connstring).DataTable(sql);

            return dt;
        }

        /// <summary>
        /// 添加搜索引擎设置
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="uri">url</param>
        /// <param name="ResultRegex">结果采集正则表达式</param>
        /// <param name="SectionRegex">分段正则表达式</param>
        /// <param name="HtmlRegex">页面信息正则表达式</param>
        /// <param name="SnapRegex">快照信息采集正则表达式</param>
        /// <param name="codepage">网页编码代码</param>
        /// <param name="codename">编码名称</param>
        /// <param name="PageRegex">页码采集正则表达式</param>
        /// <param name="CountRegex">收录量采集正则表达式</param>
        /// <returns>Int</returns>
        public static int Add(string name, string uri, string ResultRegex, string SectionRegex, string HtmlRegex, string SnapRegex, string codepage, string codename, string PageRegex, string CountRegex)
        {
            if (Exsit(name, "0")) return -2;
            string sql = "insert into [QT_Basic_SearchEngine]([sid],[SearchName],[URI],[ResultRegex],[SectionRegex],[HtmlRegex],[SnapRegex],[CodePage],[CodeName],[PageRegex],[CountRegex]) values('" + Guid.NewGuid().ToString().Replace("-", "") + "','" + name + "','" + uri + "','" + ResultRegex + "','" + SectionRegex + "','" + HtmlRegex + "','" + SnapRegex + "','" + codepage + "','" + codename + "','" + PageRegex + "','" + CountRegex + "')";
            int rval = new QT.DBO.OleDBHelper(myDB.connstring).ExecuteNonQuery(sql);
            return rval;
        }

        /// <summary>
        /// 修改搜索引擎设置
        /// </summary>
        /// <param name="id">ID</param>
        /// <param name="name">名称</param>
        /// <param name="uri">url</param>
        /// <param name="ResultRegex">结果采集正则表达式</param>
        /// <param name="SectionRegex">分段正则表达式</param>
        /// <param name="HtmlRegex">页面信息正则表达式</param>
        /// <param name="SnapRegex">快照信息采集正则表达式</param>
        /// <param name="codepage">网页编码代码</param>
        /// <param name="codename">编码名称</param>
        /// <param name="PageRegex">页码采集正则表达式</param>
        /// <param name="CountRegex">收录量采集正则表达式</param>
        /// <returns>Int</returns>
        public static int Update(string id, string name, string uri, string ResultRegex, string SectionRegex, string HtmlRegex, string SnapRegex, string codepage, string codename, string PageRegex, string CountRegex)
        {
            if (Exsit(name, id)) return -2;
            string sql = "update [QT_Basic_SearchEngine] set [SearchName]='" + name + "',[URI]='" + uri + "',[ResultRegex]='" + ResultRegex + "',[SectionRegex]='" + SectionRegex + "',[HtmlRegex]='" + HtmlRegex + "',[SnapRegex]='" + SnapRegex + "',[CodePage]='" + codepage + "',[CodeName]='" + codename + "',[PageRegex]='" + PageRegex + "',[CountRegex]='" + CountRegex + "' where [Sid]='" + id + "'";
            int rval = new QT.DBO.OleDBHelper(myDB.connstring).ExecuteNonQuery(sql);
            return rval;
        }

        /// <summary>
        /// 删除搜索引擎设置
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>Int</returns>
        public static int Delete(string id)
        {
            string sql = "delete from [QT_Basic_SearchEngine] where [sid]='" + id + "'";
            int rval = new QT.DBO.OleDBHelper(myDB.connstring).ExecuteNonQuery(sql);
            return rval;
        }

        private static bool Exsit(string name, string id)
        {
            string sql = "select [sid] from [QT_Basic_SearchEngine] where [SearchName]='" + name + "' and [sid]<>'" + id + "'";
            DataTable dt = new QT.DBO.OleDBHelper(myDB.connstring).DataTable(sql);
            if (dt.Rows.Count > 0) return true;
            return false;
        }
    }
}
